﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using TienIch;
using TienIch.Business;
using TienIch.Model;
using TienIch.Utils;

namespace TienIch
{
    public partial class frmCompany : FormBase
    {
        DataTable m_DataSource = new DataTable();
        public frmCompany()
        {
            InitializeComponent();
        }
        #region Methods
        public static void SelectAll(ref DataTable dt, GridControl grd)
        {
            try
            {
                dt = CompanyBO.Instance.FindAll();
                grd.DataSource = dt;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
        public static void Save(ref DataTable dt)
        {
            try
            {
                DataTable dtRemine;
                dtRemine = dt.GetChanges();
                if (dtRemine != null)
                {
                    for (int i = 0; i < dtRemine.Rows.Count; i++)
                    {
                        if (dtRemine.Rows[i]["ID"] == DBNull.Value) //add new
                        {
                            //code in here
                        }
                        else //update
                        {
                            //code in here
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
        #endregion
        private void frmCompany_Load(object sender, EventArgs e)
        {
            SelectAll(ref m_DataSource, gridControl1);
            PageBase.StateButton(false, btnNew, btnEdit, btnSave, btnCancel, btnDelete);
            PageBase.VisibleColumnInGrid(gridView1, false);
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            PageBase.StateButton(true, btnNew, btnEdit, btnSave, btnCancel, btnDelete);
            PageBase.VisibleColumnInGrid(gridView1, true);
            Global.sFormStatus = Global.FormStatus.Addnew;
            gridView1.FocusedRowHandle = GridControl.NewItemRowHandle;
            //gridView1.FocusedColumn = colTenCV;
            gridView1.ShowEditor();
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            PageBase.StateButton(true, btnNew, btnEdit, btnSave, btnCancel, btnDelete);
            PageBase.VisibleColumnInGrid(gridView1, true);
            Global.sFormStatus = Global.FormStatus.Edit;
            //gridView1.FocusedRowHandle = gridView1.se;
            //gridView1.FocusedColumn = colTenCV;
            //gridView1.ShowEditor();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            gridView1.FocusedRowHandle = -1;
            //Save(ref m_DataSource);
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            PageBase.StateButton(false, btnNew, btnEdit, btnSave, btnCancel, btnDelete);
            PageBase.VisibleColumnInGrid(gridView1, false);
            Global.sFormStatus = Global.FormStatus.View;
            SelectAll(ref m_DataSource, gridControl1);
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            SelectAll(ref m_DataSource, gridControl1);
            PageBase.StateButton(false, btnNew, btnEdit, btnSave, btnCancel, btnDelete);
            PageBase.VisibleColumnInGrid(gridView1, false);
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.FocusedRowHandle < 0)
                {
                    XtraMessageBox.Show("Không có mẫu tin nào để xóa", "Thông báo", MessageBoxButtons.OK);
                    return;
                }
                //int IDs = Convert.ToInt32(gridView1.GetFocusedRowCellValue(colID).ToString());
                //if (CompanyBO.Instance.CheckExist("SanPhamDAID", IDs))
                //{
                //XtraMessageBox.Show("Mẫu tin này đang được sử dụng nên không xóa được", "Thông báo", MessageBoxButtons.OK);
                //return;
                //}
                {
                    if (XtraMessageBox.Show("Bạn có chắc xóa mẫu tin này không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        int j = -1;
                        for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                        {
                            if (gridView1.GetSelectedRows()[i] >= 0)
                                j = gridView1.GetSelectedRows()[i];
                            if (j >= 0)
                            {
                                int IDs = Convert.ToInt32(gridView1.GetRowCellValue(j, colID).ToString());
                                //if (HangMucCongViecBO.Instance.CheckExist("SanPhamDAID", IDs))
                                //{
                                //XtraMessageBox.Show("Mẫu tin này đang được sử dụng, bạn không thể xóa nó", "Thông báo");
                                //return;
                                //}
                                CompanyBO.Instance.Delete(IDs);
                            }
                        }
                        SelectAll(ref m_DataSource, gridControl1);
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (e.FocusedRowHandle >= 0 & Global.sFormStatus == Global.FormStatus.View)
                {
                    Global.sIDLinkGridView = gridView1.GetFocusedRowCellValue("ID").ToString();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                //if (gridView1.GetRowCellValue(e.RowHandle, "Name").ToString() == "")
                //{
                //XtraMessageBox.Show("AAAAAAAAAAAAAA không được rỗng", "Thông báo", MessageBoxButtons.OK);
                //e.Valid = false;
                //}
                GridView view = sender as GridView;
                CompanyModel model = new CompanyModel();

                //model.ID = Convert.ToInt32(view.GetRowCellValue(e.RowHandle, colID).ToString());
                model.Code = Convert.ToString(view.GetRowCellValue(e.RowHandle, colCode).ToString());
                model.Name = Convert.ToString(view.GetRowCellValue(e.RowHandle, colName).ToString());
                model.GiamDoc = Convert.ToString(view.GetRowCellValue(e.RowHandle, colGiamDoc).ToString());
                model.KeToanTruong = Convert.ToString(view.GetRowCellValue(e.RowHandle, colKeToanTruong).ToString());
                model.ThuQuy = Convert.ToString(view.GetRowCellValue(e.RowHandle, colThuQuy).ToString());
                model.DiaChi = Convert.ToString(view.GetRowCellValue(e.RowHandle, colDiaChi).ToString());
                model.DienThoai = Convert.ToString(view.GetRowCellValue(e.RowHandle, colDienThoai).ToString());
                model.Fax = Convert.ToString(view.GetRowCellValue(e.RowHandle, colFax).ToString());
                if (view.GetRowCellValue(e.RowHandle, colID) == DBNull.Value) //add new 
                {
                    CompanyBO.Instance.Insert(model);
                }
                else //Update
                {
                    model.ID = Easy.ToInt(view.GetRowCellValue(e.RowHandle, colID).ToString());
                    if (!CompanyBO.Instance.CheckExist("ID", model.ID))
                    {
                        CompanyBO.Instance.Insert(model);
                    }
                    else
                        CompanyBO.Instance.Update(model);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
            //SelectAll(ref m_DataSource, gridControl1);
        }
    }
}
